package cn.maihe.elg.operation.web.business;

import cn.maihe.elg.operation.exception.ErrorCode;
import cn.maihe.elg.operation.exception.SystemRuntimeException;
import cn.maihe.elg.operation.model.vo.business.ListCustomerBlacklistVO;
import cn.maihe.elg.operation.repository.entity.CustomerBlacklistDO;
import cn.maihe.elg.operation.repository.entity.CustomerBlacklistVo;
import cn.maihe.elg.operation.service.business.CustomerBlacklistService;
import cn.maihe.elg.operation.utils.IdGenerate;
import cn.maihe.elg.operation.utils.UserContextUtil;
import cn.maihe.framework.starter.base.sql.PageBo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @Description 客户黑名单列表controller
 * @Author WJH
 * @Date 2021/03/25
 */
@Api(value = "客户黑名单管理", tags = "客户黑名单管理")
@RestController
@RequestMapping("/api")
@Slf4j
public class CustomerBlacklistController {
    @Resource
    private CustomerBlacklistService blacklistService;

    @ApiOperation(value = "分页查询客户黑名单", notes = "根据机构名称、投保客户名称，统一社会信用代码 分页查询")
    @RequestMapping(value = "/customerBlacklists", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNo", value = "页数", dataType = "int", paramType = "query", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页大小", dataType = "int", paramType = "query", required = true),
            @ApiImplicitParam(name = "orgName", value = "机构名称", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "customerName", value = "投保客户名称", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "customerCreditCode", value = "统一社会信用代码", dataType = "String", paramType = "query")
    })
    public PageBo<ListCustomerBlacklistVO> pageList(int pageNo, int pageSize, ListCustomerBlacklistVO vo) {
        return blacklistService.selectPageBySql(pageNo, pageSize, vo);
    }

    @ApiOperation(value = "获取客户黑名单", notes = "根据客户黑名单id获取客户黑名单")
    @ApiImplicitParam(name = "id", value = "客户黑名单id", dataType = "long", paramType = "path", required = true)
    @RequestMapping(value = "/customerBlacklist/{id}", method = RequestMethod.GET)
    public ListCustomerBlacklistVO getInfo(@PathVariable Long id) {
        ListCustomerBlacklistVO curDO = blacklistService.getDetailById(id);
        if (curDO == null) {
            throw new SystemRuntimeException(ErrorCode.NOTEXISTS, "客户黑名单id");
        }
        return curDO;
    }

    @ApiOperation(value = "新增客户黑名单", notes = "新增客户黑名单")
    @RequestMapping(value = "/customerBlacklist", method = RequestMethod.POST)
    public void createInfo(@Validated @RequestBody CustomerBlacklistVo vo) {
        //字段校验未处理
        CustomerBlacklistDO cDO = CustomerBlacklistDO.of(vo);
        cDO.setId(IdGenerate.getId());
        Long curOrgId = UserContextUtil.getCurOrgId();
        cDO.setOrgId(curOrgId);
        if (blacklistService.existsCreditCode(vo.getCustomerCreditCode(), vo.getCenterId(), curOrgId, null)) {
            throw new SystemRuntimeException(ErrorCode.EXISTS, "统一社会信用代码", vo.getCustomerCreditCode());
        }
        blacklistService.insert(cDO);
    }

    @ApiOperation(value = "修改客户黑名单", notes = "根据客户黑名单id修改客户黑名单")
    @RequestMapping(value = "/customerBlacklist", method = RequestMethod.PUT)
    public void modifyInfo(@Validated @RequestBody CustomerBlacklistVo vo) {
        CustomerBlacklistDO mDO = CustomerBlacklistDO.of(vo);
        mDO.setUserModified(UserContextUtil.getUserContext().getUsername());
        mDO.setGmtModified(new Date());
        Long curOrgId = UserContextUtil.getCurOrgId();
        if (blacklistService.existsCreditCode(vo.getCustomerCreditCode(), vo.getCenterId(),curOrgId, vo.getId())) {
            throw new SystemRuntimeException(ErrorCode.EXISTS, "统一社会信用代码", vo.getCustomerCreditCode());
        }
        blacklistService.updateById(mDO);
    }

    @ApiOperation(value = "删除客户黑名单", notes = "根据客户黑名单id删除客户黑名单")
    @ApiImplicitParam(name = "id", value = "客户黑名单id", dataType = "long", paramType = "path", required = true)
    @RequestMapping(value = "/customerBlacklist/{id}", method = RequestMethod.DELETE)
    public void deleteInfo(@PathVariable Long id) {
        blacklistService.deleteById(id);
    }


}
